<template>
	<view class="approval-list">
		<view class="ctn_header">
			<!-- <view class="filter">
				<view class="filter_title">
					<view class="title_item">
						全部类别
					</view>
					<image src="../../static/bg/TriangleActive.png" mode=""></image>
				</view>
				<view class="filter_title">
					<view class="title_item">
						状态
					</view>
					<image src="../../static/bg/TriangleActive.png" mode=""></image>
				</view>
			</view> -->
			<!-- <liuyuno-tabs  :tabData="tabs" :defaultIndex="defaultIndex" @tabClick='tabClick' /> -->
		<liuyuno-tabs :isShowTap="false" @confirm='confirmDate' @rest='restSearch'></liuyuno-tabs>
		</view>
		<!-- :start="item.start" :end="item.end" -->
		<view class="ctn_middlee">
			<view v-for="(item, index) in listData" :key="index" v-if="listData.length!=0">
				<my-Approval :status="item.appStatus" :isShowTakeBtn='item.appStatus==3' :propTitlel="item.propTitlel" 
				 :propdays="item.propTitler" :pk='item.pk' :timeArr="item.timeArr" :showBtn='item.appStatus==-1' 
				@confirm='confirmAdvice' @prograss='checkPrograss(item.pk)' @takeback='takeback(item.pk)' @deleteRec='deleteRec(item.pk)' @edit='edit(item)'></my-Approval>
			</view>
		</view>
		<view class="jumpHome" @click="jumpHome">首页</view>
	</view>
</template>

<script>
import utils from '../../common/utils/utils.js'
import service from '../../common/service/new_file.js';
import apiData from '../../common/utils/apiData.js';
export default {
	data() {
		return {
			tabs: ['待审核', '已审核'],
			defaultIndex: 0,
			form: {
				startTime: "",
				endTime: ""
			},
			leaveTypeOptions:[],
			listData: [],
			fileList:['1'],
		};
	},
	methods: {
		jumpHome(){
			uni.navigateTo({
				url:'../home/home'
			})
		},
		checkPrograss(pk){
			uni.navigateTo({
				url:'../attendance/attendacePrograss?pk='+pk+'&billtype=HA01'
			})
		},
		edit(item){
			uni.navigateTo({
				url:'../leave/leave/leave?item='+JSON.stringify(item)
			})
		},//修改内容
		
		//++++++++++++++++++++++附件的操作++++++++++++++
		//++++++++++++++++++++++附件的操作++++++++++++++
		confirmAdvice(val){
				let _this = this
				let billtype = "HA01";
				let data = apiData.assign(val.pk, val.spr, billtype,'sendapprove','Y');
				service.common(data, {
					success: res => {
						uni.showToast({
							icon: 'success',
							title: '操作成功!'
						});
						_this.getLeaveRecord(_this.form.startTime,_this.form.endTime)
					}
				});
		},
		deleteRec(pk){
			  let _this = this;
			uni.showModal({
			    title: '提示',
			    content: '你确定删除当前单据吗？',
			    success: function (res) {
			        if (res.confirm) {
			            let data = apiData.delLeaveRecord(pk,"HA01");
			            service.common(data, {
			            	success: function(res) {
			            		uni.showToast({
			            			title:'删除成功',
			            		})
			            		setTimeout(()=>{
			            			_this.getLeaveRecord(_this.form.startTime,_this.form.endTime)
			            		},500)
			            	},
			            	fail: function(res) {
			            	}
			            });
			        } else if (res.cancel) {
			            uni.showToast({
							icon:'none',
			            	title:'已取消操作',
			            })
			        }
			    }
			})
			
		},
		takeback(pk){
			let _this = this;
			let data = apiData.takebackRecord(pk,"HA01");
			service.common(data, {
				success: function(res) {
					uni.showToast({
						title:'操作成功',
					})
					setTimeout(()=>{
						_this.getLeaveRecord(_this.form.startTime,_this.form.endTime)
					},500)
				},
				fail: function(res) {
				}
			});
		},
		confirmDate(time){
			let startTime =time[0]+' '+'00:00:00'
			let endTime =time[1]+' '+"23:59:59"
			this.getLeaveRecord(startTime,endTime);
		},
		restSearch(){
			this.getLeaveRecord(this.form.startTime,this.form.endTime)
		},
		getAllLeaveType() {
			let _this = this;
			let data = apiData.getLeaveTypeData("leavetype");
			service.common(data, {
				success: function(res) {
					let leaveObj = res.data.VALUES[0];
					let leaveOption = [];
					for (let key in leaveObj) {
						leaveOption.push({
							value: key,
							label: leaveObj[key]
						});
					}
					_this.leaveTypeOptions = leaveOption;
					_this.getLeaveRecord(_this.form.startTime,_this.form.endTime);
				},
				fail: function(res) {
					console.log(res);
				}
			});
		},
		getLeaveRecord(startTime,endTime) {
			let _this = this;
			let data = apiData.getLeaveRecord("HA01",startTime,endTime);
			service.common(data, {
				success: res => {
					function compare(property){
					    return function(a,b){
					        var value1 =new Date( a[property].replace(/-/g,'/')).getTime();
					        var value2 = new Date( b[property].replace(/-/g,'/')).getTime();
					        return value2 - value1;//升序,降序为value2 - value1
					    }
					}
					function timeFormat(time){
						let date = new Date(time.replace(/-/g,'/'))
						let yy = date.getFullYear()
						let mm = date.getMonth()+1
						let dd =date.getDate()
						return yy+'-'+mm+'-'+dd
						debugger
					}
					if(res.data.VALUES.length!=0){
						// console.log(res)
						let newData  = []
						let resArr = res.data.VALUES
						// console.log(resArr)
						resArr.forEach(item=>{
							let sumhours =0
							let allTimeArr=[]
							item.data.bvos.forEach(item3=>{
								let start
								let endTime
								if(item3.leavebeginpro==1){
									start = timeFormat(item3.leavebegintime)+' '+'全天'
								}else if(item3.leavebeginpro==2){
									start = timeFormat(item3.leavebegintime)+' '+'上午'
								}else if(item3.leavebeginpro==3){
									start = timeFormat(item3.leavebegintime)+' '+'下午'
								}
								
								if(item3.leaveendpro==1){
									endTime = timeFormat(item3.leaveendtime)+' '+'全天'
								}else if(item3.leaveendpro==2){
									endTime = timeFormat(item3.leaveendtime)+' '+'上午'
								}else if(item3.leaveendpro==3){
									endTime = timeFormat(item3.leaveendtime)+' '+'下午'
								}
								allTimeArr.push({
									start:start,
									viewStarttime:start,//申请页渲染的数据
									endTime:endTime,
									viewEndtime:endTime,
									tsbody:item3.ts,
									pk_leave_b:item3.pk_leave_b,
									leavehours:item3.leavehour
								})
								sumhours+=Number(item3.leavehour)
							})
							//把时间组合成数组，以便后面做比较
							// // console.log(allTimeArr)
							// let a = item.data.bvos.length!=0?item.data.bvos[0].leavebegintime:''
							// let timeStampArr = allTimeArr.map(item=>
							// 	item = new Date(item).getTime()
							// )
							// let startTimeStamp = utils.dataFormat(Math.min(...timeStampArr))
							// let endStamp = utils.dataFormat(Math.max(...timeStampArr))
							//取最大值和最小值并转成时间格式
							
							let filterLeaveShift = _this.leaveTypeOptions.filter(item2=>item2.value==item.data.hvo.pk_leavetype.pk)
							//取到休假类别的枚举
							newData.push({
								appStatus:item.data.hvo.approvestatus,
								propTitler:sumhours,
								propTitlel:item.data.hvo.pk_leavetype.name,
								timeArr:allTimeArr,
								timeServiceArr:item.data.bvos,
								pk_leavetype:item.data.hvo.pk_leavetype,
								pk:item.data.bvos[0].pk_leave_h,
								tshead:item.data.hvo.ts,
								billno:item.data.hvo.billno,
								leaveremark:item.data.bvos[0].leaveremark,
								pk_leave_h:item.data.bvos[0].pk_leave_h,
								leaveshift:item.data.bvos[0].leaveshift,
								pk_org_v:item.data.hvo.pk_org_v,
								creator:item.data.hvo.creator,
								billmaker:item.data.hvo.billmaker,
								pk_period:item.data.hvo.pk_period,
							})
						});
						// newData.sort(compare('ts'))//排序
						_this.listData = newData
					}else{
						this.listData =[]
						uni.showToast({
							icon:'none',
							title:'当前列表为空'
						})
					}
				}
			});
		},
		tabClick(e) {
			// if(e=='0'){
			// 	console.log('全部')
			// }else if(e=='2'){
			// 	console.log('待审核')
			// }else if(e=='1'){
			// 	console.log('已审核')
			// }
		}
	},
	onReady() {
		// let month=new Date().getMonth()+1
		// if(month<10){
		// 	month='0'+month
		// }
		// this.form.startTime = new Date().getFullYear()+'-'+month+'-'+'01 01:00:00'
		// this.form.endTime = new Date().getFullYear()+'-'+month+'-'+new Date().getDate()+' '+'01:00:00'
		// this.deleteRec('1001ZZ100000000GOJM9')
		this.getAllLeaveType()
	},
	onPullDownRefresh() {
		this.getAllLeaveType()
	},
	onLoad() {
		
	}
};
</script>

<style lang="scss" scoped>
	.jumpHome{
		width: 80rpx;
		position: fixed;
		bottom: 350rpx;
		right:20rpx;
		height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 16rpx;
		background: #a7a7a7;
		color: #F1F1F1;
		font-size: 24rpx;
	}
.approval-list {
	height: 100vh;
	display: flex;
	background: #f7f7f7;
	flex-direction: column;
	.ctn_header {
	}

	.ctn_middlee {
		flex: 1;
		padding: 0 36rpx;
		margin-top: 140rpx;
	}
}
</style>
